from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.database import get_db
from app import models, schemas, auth

router = APIRouter(prefix="/users", tags=["用户"])


@router.get("/me", response_model=schemas.Response)
async def get_current_user_info(current_user: models.User = Depends(auth.get_current_user)):
    """获取当前用户信息"""
    return schemas.Response(
        data=schemas.UserResponse.from_orm(current_user)
    )


@router.patch("/me", response_model=schemas.Response)
async def update_current_user(
    request: schemas.UpdateUserRequest,
    current_user: models.User = Depends(auth.get_current_user),
    db: Session = Depends(get_db)
):
    """更新当前用户信息"""
    if request.nickname is not None:
        current_user.nickname = request.nickname
    if request.avatar_url is not None:
        current_user.avatar_url = request.avatar_url
    
    db.commit()
    db.refresh(current_user)
    
    return schemas.Response(
        message="更新成功",
        data=schemas.UserResponse.from_orm(current_user)
    )


@router.post("/avatar", response_model=schemas.Response)
async def upload_avatar(
    current_user: models.User = Depends(auth.get_current_user),
    db: Session = Depends(get_db)
):
    """上传头像（实际实现需要文件上传处理）"""
    # TODO: 实现文件上传逻辑
    # 1. 接收文件
    # 2. 验证文件类型和大小
    # 3. 保存到对象存储
    # 4. 更新用户头像URL
    return schemas.Response(message="头像上传功能待实现")

